﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace Speedy
{
    public partial class frmEvrakYonetim : XtraForm
    {
        public bool TumListe { get; set; }
        ISynchronizer sqlinstance;

        public frmEvrakYonetim()
        {
            InitializeComponent();
            string defaultval = "";
            if (Config.GetConfig.GetValue(CParams.Db).IndexOf("Mikro") > -1)
                defaultval = "Mikro V12";
            else
                defaultval = "WorkGroup";

            if (Config.GetConfig.GetValue(CParams.ProgramType, defaultval) == "WorkGroup")
                sqlinstance = new syncWorkGrup();
            else
                sqlinstance = new syncMikroV12();
        }

        private void frmEvrakYonetim_Load(object sender, EventArgs e)
        {
            TumListe = false;
            Listele(TumListe);
            gridView1.Columns["ID"].Visible = false;
        }


        //void Listele(bool tumu)
        //{
        //    Cursor.Current = Cursors.WaitCursor;
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append(" exec sp_executesql N' ");
        //    if (tumu)
        //        sb.Append(" select top 100 percent ");
        //    else
        //        sb.Append(" select top 100 ");

        //    sb.Append(" case when INVOICE_HEADER.DOCUMENT_TYPE =21 then ''Satış Faturası'' when INVOICE_HEADER.DOCUMENT_TYPE =10 then ''Alınan Sipariş'' when INVOICE_HEADER.DOCUMENT_TYPE =3 then ''Tahsilat'' else '''' end AS [Evrak Cinsi], ");
        //    sb.Append(" INVOICE_HEADER.DOCUMENT_DATE AS [Belge Tarihi],INVOICE_HEADER.INVOICE_SERIES AS [Belge Seri],INVOICE_HEADER.INVOICE_NUMBER AS [Belge Sıra], CARI_HESAPLAR.cari_kod AS [Cari Kodu],CARI_HESAPLAR.cari_unvan1 AS [Cari 1.Ünvanı], CARI_HESAPLAR.cari_unvan2 AS [Cari 2.Ünvanı], DEVICE_DEFINITIONS.ID AS [Terminal No], DEVICE_DEFINITIONS.DESCRIPTION AS [Terminal Bilgisi], INVOICE_HEADER.GENERAL_TOTAL AS Tutar, INVOICE_HEADER.SAVED_FLAG AS Kaydedildi,INVOICE_HEADER.ID,INVOICE_HEADER.DESCRIPTION AS [Bilgi] ");
        //    sb.Append(" FROM  INVOICE_HEADER INNER JOIN DEVICE_DEFINITIONS ON INVOICE_HEADER.TERMINAL_ID = DEVICE_DEFINITIONS.ID INNER JOIN CARI_HESAPLAR ON INVOICE_HEADER.CUSTOMER_CODE = CARI_HESAPLAR.cari_kod ");
        //    sb.Append(" ORDER BY [Belge Tarihi] DESC '");
        //    Cursor.Current = Cursors.Default;
        //    gridControl1.DataSource = DataLayer.GetLayer.FillData(sb.ToString());
        //}

        void Listele(bool tumu)
        {
            //Metod Adı sorulacak
            gridControl1.DataSource = DataLayer.GetLayer.FillData(sqlinstance.EvrakListesi(tumu));
        }

        private void tümKayıtlarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TumListe = true;
            Listele(true);
            gridView1.Columns["ID"].Visible = false;
        }


        void EvrakSil(int id)
        {
            using (SqlTransaction trn = DataLayer.GetLayer.OpenTransaction())
            {
                SqlParameter prm = new SqlParameter("@id", SqlDbType.Int) { Value = id };
                try
                {
                    DataLayer.GetLayer.ExecQuery("DELETE FROM INVOICE_HEADER  WHERE     (ID = @id)", trn, prm);
                    DataLayer.GetLayer.ExecQuery("DELETE FROM INVOICE_DETAILS  WHERE     (HEADER_ID =@id)", trn, prm);
                    DataLayer.GetLayer.ExecQuery("DELETE FROM RECEIVING  WHERE     (HEADER_ID =@id)", trn, prm);
                    trn.Commit();
                }
                catch (Exception)
                {
                    trn.Rollback();
                }
            }
        }


        void EvrakGuncelle(int id)
        {
            SqlParameter prm1 = new SqlParameter("@id", SqlDbType.Int) { Value = id };
            SqlParameter prm2 = new SqlParameter("@flag", SqlDbType.Bit) { Value = false };
            DataLayer.GetLayer.ExecQuery("UPDATE INVOICE_HEADER set SAVED_FLAG=@flag,DESCRIPTION=''  WHERE     (ID = @id)", prm1, prm2);
        }

        private void yeniden_aktar_Click(object sender, EventArgs e)
        {
            int[] rows = gridView1.GetSelectedRows();
            if (rows.Length > 0)
                foreach (int row in rows)
                    EvrakGuncelle(Convert.ToInt32(gridView1.GetRowCellValue(row, gridView1.Columns["ID"])));

            Listele(TumListe);
        }

        private void silToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult dlg = MessageBox.Show("Silmek İstediğinize Eminmisiniz ?", "Uyarı", MessageBoxButtons.YesNo);
            if (dlg == DialogResult.Yes)
            {
                int[] rows = gridView1.GetSelectedRows();
                if (rows.Length > 0)
                    foreach (int row in rows)
                        EvrakSil(Convert.ToInt32(gridView1.GetRowCellValue(row, gridView1.Columns["ID"])));
                Listele(TumListe);
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                Listele(TumListe);
        }

        private void evrakSeriToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int row = gridView1.FocusedRowHandle;
            if (row > -1)
            {
                frmSeriDegistir seri = new frmSeriDegistir();
                seri.RecId = Convert.ToInt32(gridView1.GetRowCellValue(row, gridView1.Columns["ID"]));
                if (seri.ShowDialog(this) == DialogResult.OK)
                    Listele(TumListe);
            }
        }
    }
}
